package com.aiss.ws.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiss.ws.Entity.AgreenonItem;
import com.aiss.ws.Entity.CurseItem;
import com.aiss.ws.R;
import com.aiss.ws.adapter.CarListAdatper;
import com.aiss.ws.httptools.GetJSONObjectPostUtil;
import com.aiss.ws.httptools.GetJsonListener;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;
import com.lidroid.xutils.exception.HttpException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang on 16/1/6.
 */
@EActivity(R.layout.activity_mycar)
public class MycarActivity extends BaseActivity {

    private static Activity act;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what== Command.HANDLECODE15){


                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    JSONArray array = obj.getJSONArray("return_data");
                    for(int i = 0 ; i < array.length();i++){
                        CurseItem item = new CurseItem();
                        item.setIdx(array.getJSONObject(i).getString("idx"));
                        item.setNamex(array.getJSONObject(i).getString("namex"));
                        item.setClass_hour(array.getJSONObject(i).getString("class_hour"));
                        item.setBuy_type_name(array.getJSONObject(i).getString("buy_type_name"));
                        item.setBuy_type_id(array.getJSONObject(i).getString("buy_type_id"));
                        item.setBuy_type_price(array.getJSONObject(i).getString("buy_type_price"));
                        item.setPrice(item.getBuy_type_price());
                        price += Double.parseDouble(item.getBuy_type_price());
                        datas.add(item);
                    }
                    adatper = new CarListAdatper(datas,context);
                    listView.setAdapter(adatper);
                    money_tv.setText("总金额:\t￥"+price);
                    black_view.setVisibility(View.VISIBLE);
                    rel_below.setVisibility(View.VISIBLE);

                    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {


                            showConfirmDialog("是否删除\""+datas.get(position).getNamex()+"\"试题?",new OnDialogConfirmListener(){
                                @Override
                                public void onDialogConfirm() {
                                    HashMap<String,String> map = new HashMap<String, String>();
                                    map.put("type","del");
                                    map.put("idx",datas.get(position).getIdx());
                                    sposition = position;
                                    Futil.xutils(Url.CART,map,handler,Command.HANDLECODE16);
                                }
                            });

                            return true;
                        }
                    });


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }else if(msg.what==Command.HANDLECODE16){
                datas.remove(sposition);
                adatper.notifyDataSetChanged();
                money_tv.setText("总金额:\t￥"+getPrice(datas));
            }

        }
    };

    /**
     * 得到总价钱
     * @param datas
     * @return
     */
    public int getPrice(List<CurseItem> datas){
        int price = 0;
        for(int i = 0 ; i < datas.size();i++){
            price += Integer.parseInt(datas.get(i).getPrice());
        }

        return price;
    }


    private int sposition = 0;

    private Double price = 0d;

    @ViewById(R.id.money_tv)
    TextView money_tv;

    @ViewById(R.id.rel_below)
    RelativeLayout rel_below;

    private Context context = this;

    private CarListAdatper adatper;

    @ViewById(R.id.black_view)
    View black_view;

    @ViewById(R.id.listView)
    ListView listView;

    @ViewById(R.id.title)
    TextView title;

    @Click(R.id.back)
    void back(){
        finish();
    }

    private List<CurseItem> datas;

    @AfterViews
    void init(){
        act = this;
        datas = new ArrayList<>();
        title.setText("购物车");
        HashMap<String,String> map = new HashMap<>();
        map.put("type","list");
        Futil.xutils(Url.CART,map,handler,Command.HANDLECODE15);
    }

    /**
     * 去支付
     */
    @Click(R.id.next_button)
    void next_button(){
        final AgreenonItem agreenonItem = new AgreenonItem();
        agreenonItem.setDatas(datas);
        HashMap<String,String> map = new HashMap<>();
        map.put("type","is_completion");
        Futil.AddHashMap(map);
        new GetJSONObjectPostUtil(Url.INFORMATION, map, new GetJsonListener() {
            @Override
            public void onSuccessed(JSONObject result) {
                try {
                    if(result.getString("state").equals("1")){

                        if(result.getString("return_data").equals("1")){
                            AgreenOnActivity_.intent(context).extra("agreenonItem",agreenonItem).start();
                        }else{
                            AddInformationctivity_.intent(context).extra("agreenonItem",agreenonItem).start();
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(HttpException error, String msg) {
                showMessage("请检查网络连接");
            }
        });
    }

    public static Activity getActivity(){
        return act;
    }


}
